﻿using kad2SimSoft.Classes;
using kad2SimSoft.Resources;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace kad2SimSoft
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int actPeriod;
        private List<Endproduct> _endproducts;
        private List<PurchasedPart> _purchasedParts;
        private List<ManufacturedPart> _manufacturedParts;
        private List<Forecast> _forecasts;
        private List<Order> _orders;
        private List<Waitinglist> _waitinglistStock;
        private List<WaitinglistWorkplace> _waitinglistWorkplace;
        private List<Workplace> _workplaces;
        private List<OrdersInWork> _ordersInWork;
        private List<Capacity> _capacity;

        private KeyData _keydata = null;

        private PurchasingPartsControl purchasingView = null;
        private WarehouseControl warehouseView = null;
        private ProductionPlanningControl producionPlanningView = null;
        private ProductionOrderControl productionOrderView = null;
        private CapacityPlanControl capacityPlanView = null;
        private XMLImportControl xmlImportControl = null;
        private ForecastControl forecastControl = null;
        private KeyDataControl keydataControl = null;

        private String selectedLanguage;

        public String SelectedLanguage
        {
            get { return selectedLanguage; }
            set { selectedLanguage = value; }
        }

        public enum VIEWTYPE
        {
            PRODUCTIONPLANNING, PURCHASING, WAREHOUSE, ORDER, CAPACITY, XMLIMPORT, FORECAST, KEYDATA
        }
        public MainWindow(List<Endproduct> endproducts, List<PurchasedPart> purchasedParts, List<ManufacturedPart> manufacturedParts, List<Forecast> forecasts, List<Order> _orders, List<Waitinglist> _waitinglistStock, List<WaitinglistWorkplace> _waitinglistWorkplace, List<Workplace> _workplaces, List<OrdersInWork> _ordersInWork)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this._endproducts = endproducts;
            this._purchasedParts = purchasedParts;
            this._manufacturedParts = manufacturedParts;
            this._forecasts = forecasts;
            this._orders = _orders;
            this._waitinglistStock = _waitinglistStock;
            this._waitinglistWorkplace = _waitinglistWorkplace;
            this._workplaces = _workplaces;
            this._ordersInWork = _ordersInWork;
            ChangeViewTo(VIEWTYPE.PRODUCTIONPLANNING);
        }

        public MainWindow()
        {
            selectedLanguage = kad2SimSoft.Properties.Settings.Default["SelectedLanguage"].ToString();
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(selectedLanguage);
            InitializeComponent();
            ChangeViewTo(VIEWTYPE.XMLIMPORT);
            this.setButtonsDisable();
        }

        public void fillProductDescriptions()
        {
            foreach (Endproduct prod in _endproducts)
            {
                prod.Description = GetResxValueByName(prod.Type + prod.Id);
            }

            foreach (ManufacturedPart prod in _manufacturedParts)
            {
                prod.Description = GetResxValueByName(prod.Type + prod.Id);
            }

            foreach (PurchasedPart prod in _purchasedParts)
            {
                prod.Description = GetResxValueByName(prod.Type + prod.Id);
            }
        }

        private string GetResxValueByName(string key)
        {
            ResourceManager rm = null;
            if (selectedLanguage.Equals("de-DE"))
            {
                rm = new System.Resources.ResourceManager("kad2SimSoft.Resources.Lokalisierung.deDescriptions", this.GetType().Assembly);
            }
            else
            {
                rm = new System.Resources.ResourceManager("kad2SimSoft.Resources.Lokalisierung.enDescriptions", this.GetType().Assembly);
            }

            var entry =
                rm.GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true)
                  .OfType<DictionaryEntry>()
                  .FirstOrDefault(e => e.Key.ToString() == key);

            var value = entry.Value.ToString();
            return value;

        }

        /// <summary>
        /// Changes view to the passed VIEWTYPE
        /// </summary>
        public void ChangeViewTo(VIEWTYPE viewType)
        {
            this.gridViewHolder.Children.Clear();

            switch (viewType)
            {
                case VIEWTYPE.PURCHASING:
                    if (this.purchasingView == null)
                    {
                        this.purchasingView = new PurchasingPartsControl(_purchasedParts, _forecasts, actPeriod);
                    }
                    this.gridViewHolder.Children.Add(purchasingView);
                    break;

                case VIEWTYPE.WAREHOUSE:
                    if (this.warehouseView == null)
                    {
                        this.warehouseView = new WarehouseControl(_purchasedParts, _manufacturedParts, _endproducts);
                    }
                    this.gridViewHolder.Children.Add(warehouseView);
                    break;

                case VIEWTYPE.PRODUCTIONPLANNING:
                    this.btExecuteTool.IsEnabled = true;
                    this.btProductionPlanning.IsChecked = true;
                    this.btForecast.IsChecked = false;
                    if (this.producionPlanningView == null)
                    {
                        this.producionPlanningView = new ProductionPlanningControl(_purchasedParts, _manufacturedParts, _endproducts, _forecasts, _waitinglistWorkplace, _workplaces, _ordersInWork);
                    }
                    this.gridViewHolder.Children.Add(producionPlanningView);
                    break;

                case VIEWTYPE.ORDER:
                    if (this.productionOrderView == null)
                    {
                        this.productionOrderView = new ProductionOrderControl(_endproducts, _manufacturedParts, _ordersInWork, _workplaces, this);
                        //TODO
                        //this.productionOrderView = new ProductionOrderControl(_orders);
                    }
                    this.gridViewHolder.Children.Add(productionOrderView);
                    break;

                case VIEWTYPE.CAPACITY:
                    if (this.capacityPlanView == null)
                    {
                        this.capacityPlanView = new CapacityPlanControl(this);
                    }
                    this.gridViewHolder.Children.Add(capacityPlanView);
                    break;

                case VIEWTYPE.XMLIMPORT:
                    this.btForecast.IsChecked = false;
                    this.btImportXML.IsChecked = true;
                    if (this.xmlImportControl == null)
                    {
                        this.xmlImportControl = new XMLImportControl();
                    }
                    this.gridViewHolder.Children.Add(xmlImportControl);
                    break;

                case VIEWTYPE.FORECAST:
                    this.btForecast.IsChecked = true;
                    this.btImportXML.IsChecked = false;
                    if (this.forecastControl == null)
                    {
                        this.forecastControl = new ForecastControl(_purchasedParts, _manufacturedParts, _endproducts, _orders, _waitinglistStock, _waitinglistWorkplace, _workplaces, _ordersInWork);
                    }
                    this.gridViewHolder.Children.Add(forecastControl);
                    break;

                case VIEWTYPE.KEYDATA:
                    if (this.keydataControl == null)
                    {
                        this.keydataControl = new KeyDataControl();
                    }
                    this.gridViewHolder.Children.Add(keydataControl);
                    break;

                default:
                    break;
            }
        }

        private void RibbonButton_Purchasing_Click(object sender, RoutedEventArgs e)
        {
            btImportXML.IsChecked = false;
            btExportXML.IsChecked = false;
            btForecast.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btProductionPlanning.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btWarehouse.IsChecked = false;
            btKeyData.IsChecked = false;
            this.ChangeViewTo(VIEWTYPE.PURCHASING);
        }

        private void RibbonButton_Warehouse_Click(object sender, RoutedEventArgs e)
        {
            btImportXML.IsChecked = false;
            btExportXML.IsChecked = false;
            btForecast.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btProductionPlanning.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btPurchasingPlanning.IsChecked = false;
            btKeyData.IsChecked = false;
            this.ChangeViewTo(VIEWTYPE.WAREHOUSE);
        }

        private void RibbonButton_ProductionPlanning_Click(object sender, RoutedEventArgs e)
        {
            btImportXML.IsChecked = false;
            btExportXML.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btForecast.IsChecked = false;
            btWarehouse.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btOrderPlanning.IsEnabled = true;
            btPurchasingPlanning.IsChecked = false;
            btKeyData.IsChecked = false;
            this.ChangeViewTo(VIEWTYPE.PRODUCTIONPLANNING);
        }

        private void RibbonButton_Order_Click(object sender, RoutedEventArgs e)
        {
            btImportXML.IsChecked = false;
            btExportXML.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btForecast.IsChecked = false;
            btWarehouse.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btProductionPlanning.IsChecked = false;
            btPurchasingPlanning.IsChecked = false;
            btKeyData.IsChecked = false;
            this.ChangeViewTo(VIEWTYPE.ORDER);
        }
        //private void RibbonButton_Order_Click(object sender, RoutedEventArgs e)
        //{
        //    btImportXML.IsChecked = false;
        //    btExportXML.IsChecked = false;
        //    btExecuteTool.IsChecked = false;
        //    btWarehouse.IsChecked = false;
        //    btCapacityPlanning.IsChecked = false;
        //    btProductionPlanning.IsChecked = false;
        //    btPurchasingPlanning.IsChecked = false;
        //    btKeyData.IsChecked = false;
        //    this.ChangeViewTo(VIEWTYPE.ORDER);
        //}
        private void RibbonButton_Forecasts_Click(object sender, RoutedEventArgs e)
        {
            btImportXML.IsChecked = false;
            btExportXML.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btPurchasingPlanning.IsChecked = false;
            btProductionPlanning.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btWarehouse.IsChecked = false;
            btKeyData.IsChecked = false;
            this.ChangeViewTo(VIEWTYPE.FORECAST);
        }

        private void RibbonButton_Capacity_Click(object sender, RoutedEventArgs e)
        {
            btImportXML.IsChecked = false;
            btExportXML.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btWarehouse.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btProductionPlanning.IsChecked = false;
            btPurchasingPlanning.IsChecked = false;
            btKeyData.IsChecked = false;
            this.ChangeViewTo(VIEWTYPE.CAPACITY);
        }

        public void RibbonButton_XMLExport_Click(object sender, RoutedEventArgs e)
        {
            btImportXML.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btWarehouse.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btProductionPlanning.IsChecked = false;
            btPurchasingPlanning.IsChecked = false;
            btKeyData.IsChecked = false;
            Xml xml = new Xml(false, this);
            xml.writeXml(this);
        }

        private void RibbonButton_ExecuteTool_Click(object sender, RoutedEventArgs e)
        {
            //if (this.Orders != null)
            //{    setButtonsEnable();
            //    ChangeViewTo(MainWindow.VIEWTYPE.ORDER);
            //}
            clearViews();
            //producionPlanningView.Button_Click_P1(null, null);
            //producionPlanningView.Button_Click_P2(null, null);
            //producionPlanningView.Button_Click_P3(null, null);
            producionPlanningView.RibbonButton_Start_P1_Click(null, null);
            producionPlanningView.RibbonButton_Start_P2_Click(null, null);
            producionPlanningView.RibbonButton_Start_P3_Click(null, null);
            btImportXML.IsChecked = false;
            btExportXML.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btWarehouse.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btPurchasingPlanning.IsChecked = false;
            btKeyData.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btProductionPlanning.IsChecked = true;
            setButtonsEnable();

            ChangeViewTo(MainWindow.VIEWTYPE.PURCHASING);
            ChangeViewTo(MainWindow.VIEWTYPE.WAREHOUSE);
            ChangeViewTo(MainWindow.VIEWTYPE.ORDER);
            ChangeViewTo(MainWindow.VIEWTYPE.CAPACITY);
            ChangeViewTo(MainWindow.VIEWTYPE.KEYDATA);
            ChangeViewTo(MainWindow.VIEWTYPE.PRODUCTIONPLANNING);
        }

        private void clearViews()
        {
            purchasingView = null;
            warehouseView = null;
            productionOrderView = null;
            capacityPlanView = null;
            keydataControl = null;
        }

        private void RibbonButton_KeyData_Click(object sender, RoutedEventArgs e)
        {
            btImportXML.IsChecked = false;
            btExportXML.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btWarehouse.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btPurchasingPlanning.IsChecked = false;
            btProductionPlanning.IsChecked = false;
            btForecast.IsChecked = false;
            btExecuteTool.IsChecked = false;
            ChangeViewTo(MainWindow.VIEWTYPE.KEYDATA);
        }

        private void RibbonButton_XMLImport_Click(object sender, RoutedEventArgs e)
        {
            btKeyData.IsChecked = false;
            btExportXML.IsChecked = false;
            btCapacityPlanning.IsChecked = false;
            btExecuteTool.IsChecked = false;
            btWarehouse.IsChecked = false;
            btOrderPlanning.IsChecked = false;
            btPurchasingPlanning.IsChecked = false;
            btProductionPlanning.IsChecked = false;
            btExecuteTool.IsChecked = false;
            this.ChangeViewTo(VIEWTYPE.XMLIMPORT);
        }

        public int ActPeriod
        {
            get { return actPeriod; }
            set { actPeriod = value; }
        }

        public ProductionOrderControl ProductionOrderView
        {
            get { return productionOrderView; }
            set { productionOrderView = value; }
        }
        public List<PurchasedPart> PurchasedParts
        {
            get { return _purchasedParts; }
            set { _purchasedParts = value; }
        }
        public List<ManufacturedPart> ManufacturedParts
        {
            get { return _manufacturedParts; }
            set { _manufacturedParts = value; }
        }
        public List<Forecast> Forecasts
        {
            get { return _forecasts; }
            set { _forecasts = value; }
        }
        public List<Order> Orders
        {
            get { return _orders; }
            set { _orders = value; }
        }
        public List<Endproduct> Endproducts
        {
            get { return _endproducts; }
            set { _endproducts = value; }
        }
        public List<Waitinglist> WaitinglistStock
        {
            get { return _waitinglistStock; }
            set { _waitinglistStock = value; }
        }
        public List<WaitinglistWorkplace> WaitinglistWorkplace
        {
            get { return _waitinglistWorkplace; }
            set { _waitinglistWorkplace = value; }
        }
        public List<Workplace> Workplaces
        {
            get { return _workplaces; }
            set { _workplaces = value; }
        }
        public ProductionPlanningControl ProducionPlanningView
        {
            get { return producionPlanningView; }
            set { producionPlanningView = value; }
        }

        public List<OrdersInWork> OrdersInWork
        {
            get { return _ordersInWork; }
            set { _ordersInWork = value; }
        }

        public List<Capacity> Capacity
        {
            get { return _capacity; }
            set { _capacity = value; }
        }
        public KeyData Keydata
        {
            get { return _keydata; }
            set { _keydata = value; }
        }

        public PurchasingPartsControl PurchasingView
        {
            get { return purchasingView; }
            set { purchasingView = value; }
        }
        public WarehouseControl WarehouseView
        {
            get { return warehouseView; }
            set { warehouseView = value; }
        }
        public XMLImportControl XmlImportControl
        {
            get { return xmlImportControl; }
            set { xmlImportControl = value; }
        }
        public ForecastControl ForecastControl
        {
            get { return forecastControl; }
            set { forecastControl = value; }
        }
        public KeyDataControl KeydataControl
        {
            get { return keydataControl; }
            set { keydataControl = value; }
        }

        public CapacityPlanControl CapacityPlanView
        {
            get { return capacityPlanView; }
            set { capacityPlanView = value; }
        }

        public void setButtonsEnable()
        {
            this.btExportXML.IsEnabled = true;
            this.btOrderPlanning.IsEnabled = true;
            this.btCapacityPlanning.IsEnabled = true;
            this.btPurchasingPlanning.IsEnabled = true;
            this.btProductionPlanning.IsEnabled = true;
            this.btWarehouse.IsEnabled = true;
            this.btKeyData.IsEnabled = true;
        }

        public void setButtonsDisable()
        {
            this.btExportXML.IsEnabled = false;
            this.btOrderPlanning.IsEnabled = false;
            this.btCapacityPlanning.IsEnabled = false;
            this.btPurchasingPlanning.IsEnabled = false;
            this.btProductionPlanning.IsEnabled = false;
            this.btWarehouse.IsEnabled = false;
            this.btKeyData.IsEnabled = false;
            this.btExecuteTool.IsEnabled = false;
            this.btForecast.IsEnabled = false;
        }

        public void setButtonsDisableStart()
        {
            this.btExportXML.IsEnabled = false;
            this.btOrderPlanning.IsEnabled = false;
            this.btCapacityPlanning.IsEnabled = false;
            this.btPurchasingPlanning.IsEnabled = false;
            this.btProductionPlanning.IsEnabled = false;
            this.btWarehouse.IsEnabled = false;
            this.btKeyData.IsEnabled = false;
            this.btExecuteTool.IsEnabled = false;
            this.btForecast.IsEnabled = false;
        }

        public void setButtonsEnabledImported()
        {
            this.btWarehouse.IsEnabled = true;
            this.btKeyData.IsEnabled = true;
            this.btForecast.IsEnabled = true;
        }

        public void setButtonsEnabledAfterForecast()
        {
            this.btProductionPlanning.IsEnabled = true;
        }

        public void setButtonsEnabledAfterProdPlanning()
        {
            this.btOrderPlanning.IsEnabled = true;

        }

        public void setButtonsEnabledAfterCapacity()
        {
            this.btPurchasingPlanning.IsEnabled = true;

        }

        public void setButtonsEnabledAfterOrderPlanning()
        {
            this.btCapacityPlanning.IsEnabled = true;
        }

        public void setButtonsDisabledAfterOrderPlanning()
        {
            this.btCapacityPlanning.IsEnabled = false;
            this.btPurchasingPlanning.IsEnabled = false;
            this.btExportXML.IsEnabled = false;
        }

        public void setButtonsDisabledAfterChangedProdPlanning()
        {
            this.btCapacityPlanning.IsEnabled = false;
            this.btPurchasingPlanning.IsEnabled = false;
            this.btOrderPlanning.IsEnabled = false;
            this.btExportXML.IsEnabled = false;
        }

        public void setButtonsDisabledAfterChangedForecast()
        {
            this.btProductionPlanning.IsEnabled = false;
            this.btCapacityPlanning.IsEnabled = false;
            this.btPurchasingPlanning.IsEnabled = false;
            this.btOrderPlanning.IsEnabled = false;
            this.btExportXML.IsEnabled = false;
        }


        private void Ribbon_Loaded(object sender, RoutedEventArgs e)
        {
            Grid child = VisualTreeHelper.GetChild((DependencyObject)sender, 0) as Grid;
            if (child != null)
            {
                child.RowDefinitions[0].Height = new GridLength(0);
            }
        }

        private void btManual_Click (object sender, RoutedEventArgs e) {
            string path = System.IO.Directory.GetCurrentDirectory() + @"\Resources\Lokalisierung\Dokumentation_Kad2SimSoft.pdf";
            System.Diagnostics.Process.Start(path);
        }
    }
}
